import axios from "axios";
import type { AxiosResponse, InternalAxiosRequestConfig } from "axios";
import { ElMessage } from 'element-plus';

// 封装axios读取实例
const Productionread = axios.create({
    baseURL: "http://localhost:5115",
    timeout: 5000,
    headers: {
        'Content-Type': 'application/json'
    }
});

// 请求拦截器
Productionread.interceptors.request.use(
    (config: InternalAxiosRequestConfig) => {
        // 在这里可以添加请求头、token等
        const token = localStorage.getItem('token');
        if (token) {
            config.headers.Authorization = `Bearer ${token}`;
        }
        return config;
    },
    (error) => {
        console.error('请求错误:', error);
        return Promise.reject(error);
    }
);

// 响应拦截器
Productionread.interceptors.response.use(
    (res: AxiosResponse) => {
        return res.data;
    },
    (error) => {
        if (error.response) {
            const { status, data } = error.response;
            switch (status) {
                case 401:
                    ElMessage.error('未授权，请重新登录');
                    // 可以在这里处理登出逻辑
                    localStorage.removeItem('token');
                    localStorage.removeItem('isLoggedIn');
                    window.location.href = '/login';
                    break;
                case 403:
                    ElMessage.error('拒绝访问');
                    break;
                case 404:
                    ElMessage.error('请求地址出错');
                    break;
                case 500:
                    ElMessage.error('服务器内部错误');
                    break;
                default:
                    ElMessage.error(data.message || '请求失败');
            }
        } else {
            ElMessage.error('网络错误，请检查网络连接');
        }
        return Promise.reject(error);
    }
);

// 封装axios写入实例
const Productionwrite = axios.create({
    baseURL: "http://localhost:5178",
    timeout: 5000,
    headers: {
        'Content-Type': 'application/json'
    }
});

// 请求拦截器
Productionwrite.interceptors.request.use(
    (config: InternalAxiosRequestConfig) => {
        // 在这里可以添加请求头、token等
        const token = localStorage.getItem('token');
        if (token) {
            config.headers.Authorization = `Bearer ${token}`;
        }
        return config;
    },
    (error) => {
        console.error('请求错误:', error);
        return Promise.reject(error);
    }
);

// 响应拦截器
Productionwrite.interceptors.response.use(
    (res: AxiosResponse) => {
        return res.data;
    },
    (error) => {
        if (error.response) {
            const { status, data } = error.response;
            switch (status) {
                case 401:
                    ElMessage.error('未授权，请重新登录');
                    localStorage.removeItem('token');
                    localStorage.removeItem('isLoggedIn');
                    window.location.href = '/login';
                    break;
                case 403:
                    ElMessage.error('拒绝访问');
                    break;
                case 404:
                    ElMessage.error('请求地址出错');
                    break;
                case 500:
                    ElMessage.error('服务器内部错误');
                    break;
                default:
                    ElMessage.error(data.message || '请求失败');
            }
        } else {
            ElMessage.error('网络错误，请检查网络连接');
        }
        return Promise.reject(error);
    }
);
//---------------------------------------------------------------------------------
// 封装axios读取实例
const QualityInspectionread = axios.create({
    baseURL: "http://localhost:5074",
    timeout: 5000,
    headers: {
        'Content-Type': 'application/json'
    }
});

// 请求拦截器
QualityInspectionread.interceptors.request.use(
    (config: InternalAxiosRequestConfig) => {
        // 在这里可以添加请求头、token等
        const token = localStorage.getItem('token');
        if (token) {
            config.headers.Authorization = `Bearer ${token}`;
        }
        return config;
    },
    (error) => {
        console.error('请求错误:', error);
        return Promise.reject(error);
    }
);

// 响应拦截器
QualityInspectionread.interceptors.response.use(
    (res: AxiosResponse) => {
        return res.data;
    },
    (error) => {
        if (error.response) {
            const { status, data } = error.response;
            switch (status) {
                case 401:
                    ElMessage.error('未授权，请重新登录');
                    // 可以在这里处理登出逻辑
                    localStorage.removeItem('token');
                    localStorage.removeItem('isLoggedIn');
                    window.location.href = '/login';
                    break;
                case 403:
                    ElMessage.error('拒绝访问');
                    break;
                case 404:
                    ElMessage.error('请求地址出错');
                    break;
                case 500:
                    ElMessage.error('服务器内部错误');
                    break;
                default:
                    ElMessage.error(data.message || '请求失败');
            }
        } else {
            ElMessage.error('网络错误，请检查网络连接');
        }
        return Promise.reject(error);
    }
);

// 封装axios写入实例
const QualityInspectionwrite = axios.create({
    baseURL: "http://localhost:5272",
    timeout: 5000,
    headers: {
        'Content-Type': 'application/json'
    }
});

// 请求拦截器
QualityInspectionwrite.interceptors.request.use(
    (config: InternalAxiosRequestConfig) => {
        // 在这里可以添加请求头、token等
        const token = localStorage.getItem('token');
        if (token) {
            config.headers.Authorization = `Bearer ${token}`;
        }
        return config;
    },
    (error) => {
        console.error('请求错误:', error);
        return Promise.reject(error);
    }
);

// 响应拦截器
QualityInspectionwrite.interceptors.response.use(
    (res: AxiosResponse) => {
        return res.data;
    },
    (error) => {
        if (error.response) {
            const { status, data } = error.response;
            switch (status) {
                case 401:
                    ElMessage.error('未授权，请重新登录');
                    localStorage.removeItem('token');
                    localStorage.removeItem('isLoggedIn');
                    window.location.href = '/login';
                    break;
                case 403:
                    ElMessage.error('拒绝访问');
                    break;
                case 404:
                    ElMessage.error('请求地址出错');
                    break;
                case 500:
                    ElMessage.error('服务器内部错误');
                    break;
                default:
                    ElMessage.error(data.message || '请求失败');
            }
        } else {
            ElMessage.error('网络错误，请检查网络连接');
        }
        return Promise.reject(error);
    }
);
export { Productionread, Productionwrite,QualityInspectionwrite,QualityInspectionread };